package com.course.business.controller.admin;/**
 * @author: lin
 */


import com.course.server.dto.CategoryDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.CategoryService;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *@Author lin
 *@Data Created in 2020/7/28 16:04
 *Description 测试类
 *@Modified By
 *@Version V1.0
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    public static final String BUSINESS_NAME = "分类";
    @Resource
    private CategoryService categoryService;
    @PostMapping("/all")
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:39
     */

    public ResponseDto all(){
        ResponseDto responseDto = new ResponseDto();
        List<CategoryDto> categoryDtoList = categoryService.all();
        responseDto.setContent(categoryDtoList);
        return responseDto;
    }

//    public ResponseDto list(@RequestBody PageDto pageDto){
//        ResponseDto responseDto = new ResponseDto();
//        categoryService.list(pageDto);
//        responseDto.setContent(pageDto);
//        return responseDto;
//    }
    /**
     * create by: lin
     * description: 保存 id有值时更新，无值时新增
     * create time: 2020/8/2 17:40
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody CategoryDto categoryDto){
        //保存校验
            ValidatorUtil.require(categoryDto.getParent(), "父id");
            ValidatorUtil.require(categoryDto.getName(), "名称");
            ValidatorUtil.length(categoryDto.getName(), "名称", 1, 50);
        ResponseDto responseDto = new ResponseDto();
        categoryService.save(categoryDto);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:58
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        categoryService.delete(id);
        return responseDto;
    }
}
